<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta content="" name="description" />
    <meta content="webthemez" name="author" />
    <title>Bootstrap Admin Theme</title>
	<!-- Bootstrap Styles-->
    <link href="../assets/css/bootstrap.css" rel="stylesheet" />

</head>
<body>
    <div id="wrapper">

            <div id="page-inner"> 

                    <div class="row">
                        <div class="col-xs-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <div class="card-title">
                                    </div>
                                </div>
                                <div class="panel-body">
                                    <form class="form-horizontal" id="userMsg">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">员工编号</label>
                                            <div class="col-sm-10">
                                                <input type="text"  disabled="disabled" class="form-control" value="${userPersonalMsg.userId }">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">员工姓名</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control"  disabled="disabled" value="${userPersonalMsg.userName }">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">所属部门</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control"  disabled="disabled" value="${userPersonalMsg.departmentName }">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">职级</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control"  disabled="disabled" value="${userPersonalMsg.userTypeName }">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">所属公司</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control"  disabled="disabled" value="${userPersonalMsg.companyName }">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">联系方式</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" id="phoneNum" onblur="checkPhone()"  value="${userPersonalMsg.phone }">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">银行帐号</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" id="bankCardNumber"  name="bankCardNumber" onblur="getNum()"  value="${userPersonalMsg.bankNumber }">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">修改密码</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" id="password" >
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">再次输入密码</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" id="repassword" >
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-sm-offset-2 col-sm-10">
                                                <button type="button" class="btn btn-warning" onclick="updateUserMsg(this)" name="${userPersonalMsg.userId }">修改</button>
                              					<button type="reset" class="btn btn-info">取消</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
               
			
			</div>
             <!-- /. PAGE INNER  -->
            </div>
         <!-- /. PAGE WRAPPER  -->
        </div>
     <!-- /. WRAPPER  -->
    <!-- JS Scripts-->
    <!-- jQuery Js -->
    <script src="../assets/js/jquery-1.10.2.js"></script>
    <script src="../js/layui/layui.js"></script>  
      <!-- Bootstrap Js -->
    <script src="../assets/js/bootstrap.min.js"></script>
    <script type="text/javascript">
  //ajax请求，修改用户
	function updateUserMsg(obj){
		var userId = obj.getAttribute("name");
		$.ajax({
			url:"${pageContext.request.contextPath}/user/updateUserInfoOne.do",
			type:"post",
			data: {"userId":userId,"password":$("#password").val(),"repassword":$("#repassword").val()},
			dataType:"json",
			success:function(data){
				console.log("请求成功");
				if(data == null || data == ""){
					console.log("两次密码不一致");
				}else{
					var str = "";
					$("#userMsg").children().remove();
				    layer.msg(data.msg);
				    str = '<div class="form-group"><label class="col-sm-2 control-label">员工编号</label><div class="col-sm-10">'
                        +'<input type="text"  disabled="disabled" class="form-control" value="'+${data.userPersonalMsg.userId }+'">'
                    	+'</div></div><div class="form-group"><label class="col-sm-2 control-label">员工姓名</label><div class="col-sm-10">'
                        +'<input type="text" class="form-control"  disabled="disabled" value="'+${userPersonalMsg.userName }+'">'
                    	+'</div></div><div class="form-group"><label class="col-sm-2 control-label">所属部门</label><div class="col-sm-10">'
                        +'<input type="text" class="form-control"  disabled="disabled" value="'+${userPersonalMsg.departmentName }+'">'
                    	+'</div></div><div class="form-group"><label class="col-sm-2 control-label">职级</label><div class="col-sm-10">'
                        +'<input type="text" class="form-control"  disabled="disabled" value="'+${userPersonalMsg.userTypeName }+'">'
                    	+'</div></div><div class="form-group"><label class="col-sm-2 control-label">所属公司</label><div class="col-sm-10">'
                        +'<input type="text" class="form-control"  disabled="disabled" value="'+${userPersonalMsg.companyName }+'">'
                    	+'</div></div><div class="form-group"><label class="col-sm-2 control-label">联系方式</label><div class="col-sm-10">'
                        +'<input type="text" class="form-control" id="phoneNum" onblur="checkPhone()"  value="'+${userPersonalMsg.phone }+'">'
                    	+'</div></div><div class="form-group"><label class="col-sm-2 control-label">银行帐号</label><div class="col-sm-10">'
                        +'<input type="text" class="form-control" id="bankCardNumber"  name="bankCardNumber" onblur="getNum()"  value="+'${userPersonalMsg.bankNumber }+'">'
                    	+'</div></div><div class="form-group"><label class="col-sm-2 control-label">修改密码</label><div class="col-sm-10">'
                        +'<input type="password" class="form-control" id="password" ></div></div><div class="form-group"><label class="col-sm-2 control-label">再次输入密码</label>'
                    	+'<div class="col-sm-10"><input type="text" class="form-control" id="repassword" >'
                    	+'</div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10">'
                        +'<button type="button" class="btn btn-warning" onclick="updateUserMsg(this)" name="'+${userPersonalMsg.userId }+'">修改</button>'
      					+'<button type="reset" class="btn btn-info">取消</button>'
                    	+'</div></div>'
					$("#userMsg").append(str);
				}
			},
			error:function(data){
				console.log(data);
			}
		});
	}
    </script>
        <script>
    	
    	
    	//得到输入的银行卡号
    	function getNum(){
    	 var bankCardNumber = $("#bankCardNumber").val();//银行卡的id应该是动态id
    	 CheckBankNo(bankCardNumber);
    	}
    	
    	
    	
    	
//银行卡号Luhn校验算法
//luhn校验规则：16位银行卡号（19位通用）: 
//1.将未带校验位的 15（或18）位卡号从右依次编号 1 到 15（18），位于奇数位号上的数字乘以 2。
//2.将奇位乘积的个十位全部相加，再加上所有偶数位上的数字。
//3.将加法和加上校验位能被 10 整除。

//bankno为银行卡号
 function luhnCheck(bankno){
     var lastNum=bankno.substr(bankno.length-1,1);//取出最后一位（与luhn进行比较）
 
     var first15Num=bankno.substr(0,bankno.length-1);//前15或18位
     var newArr=new Array();
     for(var i=first15Num.length-1;i>-1;i--){    //前15或18位倒序存进数组
         newArr.push(first15Num.substr(i,1));
     }
     var arrJiShu=new Array();  //奇数位*2的积 <9
     var arrJiShu2=new Array(); //奇数位*2的积 >9
     
     var arrOuShu=new Array();  //偶数位数组
     for(var j=0;j<newArr.length;j++){
         if((j+1)%2==1){//奇数位
             if(parseInt(newArr[j])*2<9)
             arrJiShu.push(parseInt(newArr[j])*2);
             else
             arrJiShu2.push(parseInt(newArr[j])*2);
         }
         else //偶数位
         arrOuShu.push(newArr[j]);
     }
     
     var jishu_child1=new Array();//奇数位*2 >9 的分割之后的数组个位数
     var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数
     for(var h=0;h<arrJiShu2.length;h++){
         jishu_child1.push(parseInt(arrJiShu2[h])%10);
         jishu_child2.push(parseInt(arrJiShu2[h])/10);
     }        
     
     var sumJiShu=0; //奇数位*2 < 9 的数组之和
     var sumOuShu=0; //偶数位数组之和
     var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和
     var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和
     var sumTotal=0;
     for(var m=0;m<arrJiShu.length;m++){
         sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
     }
     
     for(var n=0;n<arrOuShu.length;n++){
         sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
     }
     
     for(var p=0;p<jishu_child1.length;p++){
         sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
         sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
     }      
     //计算总和
     sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);
     
     //计算luhn值
     var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;        
     var luhn= 10-k;
     
     if(lastNum==luhn){
        console.log("验证通过");
         return true;
     }else{
        layer.msg("银行卡号必须符合luhn校验");
         return false;
     }        
 }
 //使用layui声明
 layui.use('layer', function(){
  			var layer = layui.layer;
  		});  

 //检查银行卡号
 function CheckBankNo(bankno) {
     var bankno = bankno.replace(/\s/g,'');
     if(bankno == "") {
         layer.msg("请填写银行卡号");
         return false;
     }
     if(bankno.length < 16 || bankno.length > 19) {
         layer.msg("银行卡号长度必须在16到19之间");
         return false;
     }
     var num = /^\d*$/;//全数字
     if(!num.exec(bankno)) {
         layer.msg("银行卡号必须全为数字");
         return false;
     }
     //开头6位
     var strBin = "10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";
     if(strBin.indexOf(bankno.substring(0, 2)) == -1) {
         layer.msg("银行卡号开头6位不符合规范");
         return false;
     }
     //Luhn校验
     if(!luhnCheck(bankno)){
         return false;
     }
     return true;
 }
 
 
 
 //验证电话号码
 function checkPhone(){
 	var phoneNum = $("#phoneNum").val();
 	if(phoneNum == "" ){ 
        layer.msg("手机号码为空，请填写!");
        return;
    }else if(!(/^1[34578]\d{9}$/.test(phoneNum))){ 
        layer.msg("手机号码有误，请重填!");  
      
    } 
 }
    </script>

   
</body>
</html>
